<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6">
              <a-form-item label="填报单位">
                <a-select
                  v-model="queryParam.deptId"
                  style="width: 100%"
                  placeholder="请选择填报单位"
                  allow-clear
                  @keyup.enter.native="handleQuery"
                >
                  <a-select-option
                    v-for="item in deptListOptions"
                    :key="item.id"
                    :value="item.id"
                  >
                    {{ item.deptName }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="年份">
                <a-date-picker
                  v-model="queryParam.times"
                  mode="year"
                  :format="yearFormat"
                  :open="yearShowOne"
                  @openChange="openChangeOne"
                  @panelChange="panelChangeOne"
                  style="width: 100%"
                  allow-clear
                />
              </a-form-item>
            </a-col>

            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-year-dept-add-form
        v-if="showAddModal"
        ref="bizYearDeptAddForm"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-year-dept-edit-form
        v-if="showEditModal"
        ref="bizYearDeptEditForm"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="年报管理"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizYearDept-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizYearDeptList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type="primary"
            @click="handleAdd"
            v-hasPermi="['biz:bizYearDept:add']"
          >
            <a-icon type="plus" />新增
          </a-button>
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizYearDept:export']"
          >
            <a-icon type="download" />导出
          </a-button>
          <!-- <a-button
            type="danger"
            v-if="!multiple"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:bizYearDept:remove']"
          >
            <a-icon type="delete" />删除
          </a-button> -->
        </div>

        <span slot="operation" slot-scope="{ text, record }">
          <file-upload
            :fileList="fileList"
            @afterUpload="uploadCompleteFile"
            @removeFile="handlerRemoveFile"
            v-hasPermi="['biz:bizYearDept:upload']"
            :id="record.id"
            v-if="record.status == '0'"
          ></file-upload>
          <a v-if="record.status == '1'" @click="handleUpdate(record)">
            修 改
          </a>
          <!-- <span v-hasPermi="['biz:bizYearDept:xiangqing']">
            <span v-if="record.fileNames">
              <p
                v-for="(item, index) in record.fileNames.split(',')"
                :key="index"
              >
                <a :href="record.fileUrls.split(',')[index]" target="_blank">
                  {{ item }}
                </a>
              </p>
            </span>

          </span> -->
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
        <span slot="fileName" slot-scope="{ text, record }">
          <a :href="record.fileUrl" target="_blank">
            {{ text }}
          </a>
        </span>
        <span slot="times" slot-scope="{ text }"> {{ text }}年 </span>
        <span slot="status" slot-scope="{ text }">
          <a-tag v-if="text == '0'">未填报</a-tag>
          <a-tag v-else color="#87d068">已填报</a-tag>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizYearDept,
  delBizYearDept,
  exportBizYearDept,
  updateBizYearDept,
} from "@/api/biz/bizYearDept";
import { alllist } from "@/api/system/dept";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizYearDeptAddForm from "@/views/biz/yeardept/modules/BizYearDeptAddForm";
import BizYearDeptEditForm from "@/views/biz/yeardept/modules/BizYearDeptEditForm";
import FileUpload from "@/components/pt/fileupload/IndexList";

export default {
  name: "BizYearDept",
  components: {
    AdvanceTable,
    BizYearDeptAddForm,
    BizYearDeptEditForm,
    FileUpload,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      yearShowOne: false,
      deptListOptions: [],
      fileList: [],
      yearFormat: "YYYY",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 年报管理表格数据
      bizYearDeptList: [],
      form: {
        id: undefined,
        status: undefined,
        fileName: undefined,
        fileUrl: undefined,
      },
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,
        times: undefined,
      },
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "65px",
          scopedSlots: { customRender: "rowindex" },
        },

        {
          title: "单位",
          dataIndex: "deptName",
          ellipsis: true,
          align: "left",
          width: "20%",
        },

        {
          title: "年份",
          dataIndex: "times",
          ellipsis: true,
          align: "left",
          width: "10%",
          scopedSlots: { customRender: "times" },
        },
        {
          title: "类型",
          dataIndex: "type",
          align: "center",
          width: "10%",
        },

        {
          title: "发布时间",
          dataIndex: "createTime",
          align: "center",
          width: "15%",
        },
        {
          title: "更新时间",
          dataIndex: "updateTime",
          align: "center",
          width: "15%",
        },
        {
          title: "更新人",
          dataIndex: "updateBy",
          align: "center",
          width: "8%",
        },
        {
          title: "状态",
          dataIndex: "status",
          align: "center",
          width: "8%",
          scopedSlots: { customRender: "status" },
        },
        {
          title: "附件资料",
          dataIndex: "fileName",
          align: "center",
          width: "15%",
          ellipsis: true,
          scopedSlots: { customRender: "fileName" },
        },
        {
          title: "操作",
          dataIndex: "operation",
          align: "left",
          width: "10%",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    uploadCompleteFile(item, items, id) {
      console.log(("item", item, items));
      this.form.fileUrl = item.url;
      this.form.fileName = item.fileName;
      this.form.status = "1";
      this.form.id = id;
      this.fileList = items;
      const saveForm = JSON.parse(JSON.stringify(this.form));
      if (item.url) {
        updateBizYearDept(saveForm).then((res) => {
          this.$message.success("上传成功", 3);
          this.fileList = [];
          this.getList();
        });
      }
    },
    handlerRemoveFile(file) {
      this.$message.success("删除成功", 3);
    },

    getDeptList() {
      alllist({ treeLevel: 2 }).then((response) => {
        this.deptListOptions = response.data;
      });
    },
    /** 查询年报管理列表 */
    getList() {
      this.loading = true;
      listBizYearDept(this.queryParam).then((response) => {
        this.bizYearDeptList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd() {
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizYearDeptAddForm.handleAdd());
    },
    handleUpdate(record, ids) {
      console.log("123213", record);
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizYearDeptEditForm.handleUpdate(record, ids)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizYearDeptIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizYearDept(bizYearDeptIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizYearDept(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
    openChangeOne(status) {
      if (status) {
        this.yearShowOne = true;
      }
    },
    // 得到年份选择器的值
    panelChangeOne(value, mode) {
      console.log(value.format("YYYY"), mode);
      this.queryParam.times = value.format("YYYY");
      this.yearShowOne = false;
    },
  },
};
</script>
